package terra.model.object;

import terra.Config;
import terra.model.World;
import terra.model.object.interfaces.Interactable;
import terra.model.object.interfaces.Touchable;
import terra.network.packet.Packet;
import terra.network.packet.server.StaticInfo;

public class ItemObject extends StaticObject implements Interactable, Touchable{
	private static final long serialVersionUID = -7228975696891823328L;

	private int itemCount;
	public boolean isDropped = false;
	private boolean isPickupable = true;

	public ItemObject(int objectId, int defId) {
		super(objectId,defId);
	}

	public ItemObject() {
		super();
	}

	@Override
	public void onClick(int button, Player player) {
		if (distanceTo(player) < Config.tileSize * 2 && !isRemove()
				&& isPickupable)
			if (player.inv.pickupItem(getDefId(),getItemCount())) {
				World.getInstance().removeObject(this);
				removeObject();
			}
	}



	public boolean isPickupable() {
		return isPickupable;
	}

	public void setPickupable(boolean isPickupable) {
		this.isPickupable = isPickupable;
	}

	@Override
	public Packet getInfo() {
		StaticInfo info = new StaticInfo();
		info.x = getX();
		info.y = getY();
		info.id = getDefId();
		info.oId = getObjectId();
		info.type = StaticInfo.ITEM;
		// System.out.println("getItemInfo " + getX() +"_" + getY() + " id " +
		// info.id + "  oId " + info.oId);
		return info;
	}

	public int getItemCount() {
		return itemCount;
	}

	public void setItemCount(int itemCount) {
		this.itemCount = itemCount;
	}

	@Override
	public void applyDef() {
	}

	@Override
	public void onTouch(GameObject object) {
		if (Config.autoPickup && !isRemove() && !isDropped)
			if (object instanceof Player)
				if (((Player) object).inv.pickupItem(getDefId(),getItemCount())) {
					World.getInstance().removeObject(this);
					removeObject();
				}
	}
	
	@Override
	public boolean isSolid(GameObject object) {
		return false;
	}

}
